package com.bruip.reggie.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回类
 *
 * @author 王若龙
 * @date 2022/10/04
 */
@Data
@ApiModel("通用的返回结果封装类")
public class BaseResponse<T> implements Serializable {

    /**
     * 编码：1成功，0和其它数字为失败
     */
    @ApiModelProperty("状态码")
    private Integer code;

    /**
     * 返回信息
     */
    @ApiModelProperty("返回信息")
    private String msg;

    /**
     * 数据
     */
    @ApiModelProperty("响应数据")
    private T data;

    /**
     * 动态数据
     */
    private Map map = new HashMap();

    public static <T> BaseResponse<T> success(T object) {
        BaseResponse<T> response = new BaseResponse<T>();
        response.data = object;
        response.code = 1;
        return response;
    }

    public static <T> BaseResponse<T> error(String msg) {
        BaseResponse response = new BaseResponse();
        response.msg = msg;
        response.code = 0;
        return response;
    }

    public BaseResponse<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }

}
